package com.xlhj.rabbit.controller;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @Author: caijliu
 * @Date: 04/09/2021 14:24
 * @Description TODO
 * @Vsersion: 0.0.1
 */
@RestController
@RequestMapping("/ttl-msg")
public class TtlMsgQueueProducerController {

    private static final String TTL_MSG_QUEUE_EXCHANGE = "ttl-msg-queue-exchange";
    private static final String TTL_MSG_QUEUE_ROUT_KEY = "ttl_msg_queue-routing-key";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("sendMessage/{message}/{ttlTime}")
    public void sendMessage(@PathVariable String message, @PathVariable String ttlTime) {
        String uuid = UUID.randomUUID().toString();
        CorrelationData correlationData = new CorrelationData(uuid);
        rabbitTemplate.convertAndSend(TTL_MSG_QUEUE_EXCHANGE, TTL_MSG_QUEUE_ROUT_KEY, message, processor -> {
            processor.getMessageProperties().setExpiration(ttlTime);
            return processor;
        }, correlationData);
    }
}
